package com.tools.demo.pbTools;

import com.tools.demo.ocr.InvoiceOcr;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.Base64Utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * base64相关工具
 */
public class Base64Tools {


    /**
     * 图片转base64字符串
     *
     * @param imgFile 图片路径
     * @return
     */
    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    /**
     * base64编码字符串转换为图片,并写入文件
     *
     * @param imgStr base64编码字符串
     * @param path   图片路径
     * @return
     */
    public static boolean base64StrToImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //文件夹不存在则自动创建
            File tempFile = new File(path);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(tempFile);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 文件转base64
     * @param filePath
     * @return
     */
    public static  String fileToBase64(String filePath) {
        String imgFilePath = filePath;
        String[] res = imgFilePath.split("\\.");
        String pos = res[res.length - 1];
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data1 = Base64Utils.encodeToString(data);
        return data1;
    }

    /**
     * 将base64字符解码保存文件
     *
     * @param base64Code base64字符串
     * @param targetPath 文件地址
     * @throws Exception
     */
    public static void decodeBase64File(String base64Code, String targetPath) throws Exception {
        File file = new File(targetPath);
        if (!file.getParentFile().exists()) {
            file.mkdirs();
        }
        byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
    }


    public static void main(String[] args) throws Exception {
//        String img = "D:\\workspace\\Tools_Demo\\static\\发票.jpg";
////        System.out.println(imageToBase64Str(img));
//
//        // 获取base64
//        File file = new File("D:\\workspace\\Tools_Demo\\static\\fapiao.txt");
//        String imgBase64 = imageToBase64Str(img);
//        String toImg = "D:\\workspace\\Tools_Demo\\static\\发票2.jpg";
//        base64StrToImage(imgBase64, toImg);

        String path = "C:\\Users\\75197\\Desktop\\张欣然简历.pdf";
        System.out.println(fileToBase64(path));
    }
}
